package com.romotc.qqrobot.webqq.qqInterfaceCatching;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.romotc.common.utils.GZipUtil;
import com.romotc.joyin.bean.CustGroup;
import com.romotc.joyin.bean.CustGroupService;
import com.romotc.joyin.bean.Customer;
import com.romotc.joyin.bean.CustomerService;
import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.bean.QQUserFriend;
import com.romotc.qqrobot.bean.QQUserFriendService;
import com.romotc.qqrobot.bean.json.QQUserFriendJsonObject;
import com.romotc.scache.RequestSpy;
import com.romotc.scache.WebConnection;
/**
 * 虚拟QQ号码/客服QQ号码获取好友拦截器
 * @author Administrator
 *
 */
public class Get_user_friends2_ForMemCustService_Catching extends QQUrlBodyCatching {

	@Override
	String getHackReqBodyStr(RequestSpy requestSpy, Map<String, Object> var) {
		//首先判断下是否是虚拟工号
		QQLogedin curQQ = QQLogedin.getCurQqLogedIn(requestSpy);
		if(curQQ == null || curQQ.getCsId() < 1) {
			return null;//不是虚拟工号则继续调腾讯
		}
		
		ObjectMapper mapper = new ObjectMapper();
		
		//查询授权的客户 并转换成好友
		List<QQUserFriend> userFriendList = QQUserFriendService.qryGrantFriends(curQQ);

		//3.将对象解析为jsonObject
		QQUserFriendJsonObject qqUserFriendJsonObject = QQUserFriendJsonObject
			.QQUserFriendJsonObject(userFriendList);
		
		//4.jsonObject解析为string
		try {
			String origResPonseBody = mapper.writeValueAsString(qqUserFriendJsonObject);
			if(StringUtils.isNotBlank(origResPonseBody)) {
				//gzip压缩
//				return GZipUtil.compress(origResPonseBody);
				return origResPonseBody;
			}
			
			
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//没有好友则返回空
		return "{\"retcode\":0,\"result\":{\"friends\":[],\"marknames\":[],\"categories\":[],\"vipinfo\":[],\"info\":[]}}";
	}

	@Override
	ArrayList<String> hackRequestHeader(RequestSpy requestSpy, int bodySize,
			Map<String, Object> var) {
		/**
Connection:close
Content-Encoding:gzip
Content-Type:application/json;charset=utf-8
Date:Tue, 31 Jan 2012 12:52:12 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
Vary:Accept-Encoding
		 */
		ArrayList<String> headers = new ArrayList<String>();
		headers.add("Connection:close");
//		headers.add("Content-Encoding:gzip");//gzip压缩
		headers.add("Content-Type:application/json;charset=utf-8");
		headers.add("Content-Type:text/plain; charset=utf-8");
		headers.add("Date:Tue, 31 Jan 2012 12:52:12 GMT");
		headers.add("Server:Apache-Coyote/1.1");
//		headers.add("Transfer-Encoding:chunked");
		headers.add("Vary:Accept-Encoding");
		headers.add("Content-Length:" + bodySize);

		headers.add("dataFrom: Get_user_friends2_ForMemCustService_Catching");
		return headers;
	}

	@Override
	boolean isGoOn(RequestSpy requestSpy) {
		return false;
	}

	@Override
	public boolean modifyResponse(WebConnection wc, RequestSpy rqSpy)
			throws IOException {
		return false;
		
	}
	
}
